package cn.support.neo.mvp.core.service.intercaptor;

import android.text.TextUtils;

import java.io.IOException;

import cn.support.neo.mvp.core.provider.TokenProvider;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Name:身份验证拦截器
 * Author: WK（wangkun@xjzhicheng.com)
 * Date: 2016-1-06
 * Describe: PLEASE TO FIXME...
 */
public class OAuthInterceptor implements Interceptor {

    private TokenProvider tokenProvider;

    public OAuthInterceptor(TokenProvider tokenProvider) {
        this.tokenProvider = tokenProvider;
    }

    private String getToken() {
        if (tokenProvider != null) {
            return tokenProvider.getToken();
        }
        return null;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        if (tokenProvider != null) {
            String token = getToken();
            if (!TextUtils.isEmpty(token)) {
                //"Accept", "application/json"
                //builder.addHeader("Accept", "text/xml");
                //builder.addHeader("X-Client-Platform", "Android");
                builder.addHeader("token", token);
            }
        }
        return chain.proceed(builder.build());
    }
}
